﻿
namespace ITSSP.Models
{
	using System;
    using System.ComponentModel.DataAnnotations;
    using System.Windows.Ria;

    using ITSSP.Web.Resources;
    using ITSSP.Resources;

    /// <summary>
    ///     This internal entity is used to ease the binding between the UI controls
    ///     (DataForm and the label displaying a validation error) and the log on
    ///     credentials entered by the user
    /// </summary>
    public class ChangePasswordInfo : Entity
	{
		#region Properties
		[Required]
        [Display(Name = "OldPasswordLabel", ResourceType = typeof(ApplicationStrings))]
		public string OldPassword { get; set; }

		[Required]
        [Display(Name = "PasswordLabel", ResourceType = typeof(ApplicationStrings))]
        public string Password { get; set; }

		[Required]
        [Display(Name = "PasswordConfirmationLabel", ResourceType = typeof(ApplicationStrings))]
		[CustomValidation(typeof(ChangePasswordInfo), "CheckPasswordConfirmation")]
        public string PasswordConfirmation { get; set; }
		#endregion

		public void Reset()
		{
			this.OldPassword = "";
			this.Password = "";
			this.PasswordConfirmation = "";
		}

		#region Validation
		public static ValidationResult CheckPasswordConfirmation(string passwordConfirmation, ValidationContext validationContext)
		{
			ChangePasswordInfo user = (ChangePasswordInfo)validationContext.ObjectInstance;

			if ((user.Password == passwordConfirmation) || (Convert.ToString(user.Password) == passwordConfirmation)) {
				return ValidationResult.Success;
			}

			return new ValidationResult("Password confirmation did not match.");
		}
		#endregion
	}
}
